package com.token.task;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import com.token.utils.JWTUtil;
import com.token.utils.SystemInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenming
 * @description
 * @create: 2022-08-09
 */
@EnableScheduling
@Component
@Slf4j
public class VerifyTask {

    public static String VerifyResultStr = "";
    public static final String split = "&";
    private static final DES des = new DES(Mode.CBC, Padding.PKCS5Padding, JWTUtil.APPLY_KEY.getBytes(), JWTUtil.DES_KEY.getBytes());

    String property = System.getProperty("user.dir");
    File license = new File(property + "/license.lic");
    File apply = new File(property + "/apply.lic");

    @Scheduled(cron = "0/3 * * * * ?")
    public void task() throws IOException {

        if (license.exists()) {
            FileInputStream fis = new FileInputStream(license); //创建文件输入流
            byte[] buffer = new byte[1024]; //创建文件输入缓冲区
            ByteArrayOutputStream bos = new ByteArrayOutputStream(); //创建内存输出流
            int len;
            while ((len = fis.read(buffer)) != -1) { //当整个循环结束后，文件中的内容就全部写入了缓冲区
                bos.write(buffer, 0, len);
            }
            String jwt = Base64.decodeStr(bos.toString());
            fis.close();

            String sign = SystemInfoUtil.getSystemInfo().toString();

            String block1 = des.encryptBase64(RandomUtil.randomString(8));
            String block2 = des.encryptBase64(RandomUtil.randomString(8));
            String block3 = des.encryptBase64(RandomUtil.randomString(8));
            String block4 = des.encryptBase64(RandomUtil.randomString(8));
            String block5 = des.encryptBase64(RandomUtil.randomString(8));

            String t1 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 3 + 1));
            String t2 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 4 + 2));
            String t3 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 5 + 3));
            String t4 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 6 + 4));

            String e1 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 3));
            String e2 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 4));
            String e3 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 5));
            String e4 = des.encryptBase64(String.valueOf(RandomUtil.randomInt(100) * 6));
            StringBuilder sb = new StringBuilder();
            if (JWTUtil.verifyToken(jwt, sign)) {
                if (apply.exists()) {
                    apply.delete();
                }
                List<String> ss = new ArrayList<>(Arrays.asList(block1, t1, block2, t2, block3, t3, block4, t4, block5));
                ss.forEach(item->sb.append(item).append(split));
                VerifyResultStr = sb.toString();
            } else {
                List<String> ss = new ArrayList<>(Arrays.asList(block1, e1, block2, e2, block3, e3, block4, e4, block5));
                ss.forEach(item->sb.append(item).append(split));
                VerifyResultStr = sb.toString();
            }
        } else {
            VerifyResultStr = "";
        }
    }

}
